<?php
function renderMessage($message){
	GLOBAL $loc_string;
	$text = nl2br(parseMarkdown($message['text']));

	if(!is_numeric(strpos($text,"href")) ){
		$text = preg_replace(
		  "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
		  "<a href=\"\\0\" class='link' target=\"_blank\">\\0</a>", 
		  $text);
	}

	$urlpreview = "";
	$file = "";
	$image = "";
	$attachedimage = "";
	
	if(!empty($message['fname'])){
		if (is_numeric(strpos($message['ftype'],"image"))){
			if ($message['fsize'] >= 1000000){
				$filecontent = PDO_FetchAll("SELECT preview FROM attachment where message_id = ".$message['id'])[0]['preview'];
			} else {
				$filecontent = PDO_FetchAll("SELECT content FROM attachment where message_id = ".$message['id'])[0]['content'];
			}
			
			$attachedimage  = "<div style='position:relative;'><img class='attachedimage' style='cursor:pointer; max-width:300px; max-height:400px;' id='".$message['id']."' onClick='imageview(".$message['id'].")' src='data:".$message['ftype'].";base64,". $filecontent."'> <div class='downloadarrow' style='font-family:icons;' title='".$loc_string['tooltip_download']."' onClick='downloadfile(".$message['id'].")'></div> </div>";
		} else {
			$file = "<div class='attachment' title='".$loc_string['tooltip_download']."' onClick='downloadfile(".$message['id'].")'> <div class='attachment_icon' style='font-family:icons;'><center></center></div> <span style='min-width:200px; max-width:300px; line-height:14px; font-size:12px;'>".$message['fname']."<br><span style='color:cyan;'>".intval($message['fsize']/1024)." kb</span></span></div>";
		}
	}
	
	if(!empty($message['title'])){
		$image = "";
		if(!empty($message['image'])){
			$image = "<div style='display:inline-block; width:100%, height:100%; background-image:url(".$message['image']."); background-size:cover; background-position:center;'><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>";
		}
		
		
		$urlpreview = "<div class=\"link-preview\">
			$image
			<div class=\"link-details\">
				<div class=\"link-title\">".html_entity_decode($message['title'])."</div>
				<div class=\"link-url\">".substr($message['desc'],0,100)."</a></div>
				<div class=\"link-url\"><a href='".$message['url']."'>".$message['site']."</a></div>
			</div>
		</div><br>";
	}

	$matches = [];
	preg_match_all('/^>>.*/m', html_entity_decode($text), $matches);
	
	if (!empty($matches[0])){
		$todos = PDO_FetchAll("SELECT * FROM todo where message_id = ".$message['id']);
		$c = 0;
		foreach ($matches[0] as $match){
			$match = htmlentities(trim(str_replace("<br />","",$match)));
			$text = str_replace_once($text,$match,"<input type='checkbox' ".($todos[$c]['completed'] == "true" ? "checked" : "")." onClick='toggleTodo(\"".$todos[$c]['id']."\")'> ".$todos[$c]['text']);
			$c++;
		}
	}

	return "<div class='message received' id='".$message['id']."'>
			<div class='bubble ".($message['highlight'] == 1 ? "highlighted" : "")."'>
				$attachedimage $file $urlpreview $text<br>
				<div class='subtext'>
					<span class='revealControls'>  </span>
					<span class='controls'>
						<a title='".$loc_string['tooltip_delete']."' onClick='deletemsg(".$message['id'].")'></a> 
						<a title='".$loc_string['tooltip_edit']."' onClick='editmsg(".$message['id'].")'></a> 
						".( $message['highlight'] == 1 ? "<a style='color:red;' title='".$loc_string['tooltip_unhighlight']."' onClick='unhighlight(".$message['id'].")'></a>" : "<a title='".$loc_string['tooltip_highlight']."' onClick='highlight(".$message['id'].")'></a>")." 
						".( $message['pinned'] == 1 ? "<a style='color:red;' title='".$loc_string['tooltip_unpin']."' onClick='unpinmessage(".$message['id'].")'></a>" : "<a title='".$loc_string['tooltip_pin']."' onClick='pinmessage(".$message['id'].")'></a>")."
					</span>
					".date("d/m/y h:i", $message['date'])."
					".(!empty($message['reminder']) ? "<span style='cursor:pointer; font-family:icons; color:".($message['reminder'] < time() ? "red" : "green")."' title='".$loc_string['tooltip_reminder_set']." ".date("d/m/y h:i",$message['reminder'])."'></span>" : "")."
					".(!empty($message['pinned']) ? "<span style='cursor:pointer; font-family:icons; color:red;' title='".$loc_string['tooltip_pinned_message']."'></span>" : "")."
				</div>
			</div>
		</div>";
}
?>
